//
//  LoginViewController.m
//  lhcx
//
//  Created by 赵永杰 on 2017/5/9.
//  Copyright © 2017年 zhaoyongjie. All rights reserved.
//

#import "LoginViewController.h"
#import "VerifyViewController.h"
#import "KLCPopup.h"
#import <SMS_SDK/SMSSDK.h>
#import "WithDrawViewController.h"
#import "UnCompleteViewController.h"


@interface LoginViewController ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconTop;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconWidth;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconHeight;


@property (weak, nonatomic) IBOutlet UITextField *mobileTF;

@property (weak, nonatomic) IBOutlet UIButton *nextBtn;

@end

@implementation LoginViewController

#pragma mark - LifeCycle

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupChildViews];
    
    [self.mobileTF becomeFirstResponder];
    
//    [self.navigationController pushViewController:[[UnCompleteViewController alloc]init] animated:YES];
}


#pragma mark - Public

#pragma mark - Private

- (void)setupChildViews {
    [self.mobileTF addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
}

- (void)updateViewConstraints {
    [super updateViewConstraints];
    
    if (kScreenW > 320) {
        self.iconTop.constant = kScreenH * 0.14;
        self.iconWidth.constant = kScreenW * 0.3;
        self.iconHeight.constant = kScreenW * 0.3 * 187/130;
    }
    
}

- (IBAction)nextBtnAction:(UIButton *)sender {
    
    if ([NSString hl_isMobileNumber:self.mobileTF.text]) {
        if (LHJudgeIsTestAccount(self.mobileTF.text) || [self.mobileTF.text isEqualToString:@"18538191908"]) {
            
            [self defaultLogin];
            
        }else{
            
            [self requestMobileCode];
        }

    }else{
        // 手机号位数不够11位
        [HLProgressHUD showHudTipStr:@"请输入正确的手机号"];
    }

}

- (void)textFieldChanged:(UITextField *)textField {
    NSInteger length = textField.text.length;
    if (length > 11) {
        textField.text = [textField.text substringToIndex:11];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length == 0) {
        self.nextBtn.backgroundColor = GRAYCOLOR;
        self.nextBtn.userInteractionEnabled = NO;
    }else{
        self.nextBtn.backgroundColor = ORANGCOLOR;
        self.nextBtn.userInteractionEnabled = YES;
    }
}

#pragma mark - 与服务器交互

- (void)defaultLogin {
    VerifyViewController *vc = (VerifyViewController *)LHGetSB_VC(@"Verify");
    vc.mobile = self.mobileTF.text;

    [self.navigationController pushViewController:vc animated:YES];
}

- (void)requestMobileCode {

    LHPC_WEAK_SELF;
    [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleLoading status:@"正在发送验证码"];
    [SMSSDK getVerificationCodeByMethod:0 phoneNumber:self.mobileTF.text zone:@"86" result:^(NSError *error) {
        LHPC_STRONG_SELF;
        [HLProgressHUD dismissHUD];
        if (!error) {
            NSLog(@"获取验证码成功");
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleSuccess status:@"发送成功"];
            VerifyViewController *vc = (VerifyViewController *)LHGetSB_VC(@"Verify");
            vc.mobile = strongSelf.mobileTF.text;
            [strongSelf.navigationController pushViewController:vc animated:YES];
            
        } else {
            [HLProgressHUD showHUDWithStyle:HLProgressHUDStyleError status:@"验证码发送上限"];
        };
    }];
    
}

#pragma mark - UITableViewDelegate

#pragma mark - Getter

#pragma mark - Setter

@end
